<?php
class BinaryTreeOutput
{
    /**
     * 输出整棵树或其中一颗子树
     * @param BinaryNode $node
     * @return string
     */
    public function fetch($node=null)
    {
        return $this->fetchNode($node,"",0)."\r\n";
    }
    /**
     * 递归的输出一个节点
     * @param AvlNode $node
     * @param string $prefix
     * @param int $site 0,root;1,left;2,right
     * @return string
     */
    protected function fetchNode($node,$prefix,$site)
    {
        if($site === 0){
            $current_prefix=$prefix;
            $sub_prefix=$prefix;
        }
        elseif($site === 1){
            $current_prefix=$prefix."　┣";
            $sub_prefix=$prefix."　┇";
        }
        else{
            $current_prefix=$prefix."　┗";
            $sub_prefix=$prefix."　　";
        }
        if($node === null){
            return $current_prefix."NULL\r\n";
        }
        $output  = $current_prefix.$node."\r\n";
        $output .= $this->fetchNode($node->left,$sub_prefix,1);
        $output .= $this->fetchNode($node->right,$sub_prefix,2);
        return $output;
    }
}
